/**
 * 
 */
package com.nh.actionPhone;

import java.util.List;

import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.nh.entity.NgNewsCategory;
import com.nh.entity.NgNewsInfo;
import com.nh.entity.NgNewsInfoContentHtml;
import com.nh.entity.NgNewsInfoContentPicture;
import com.nh.entity.NgNewsInfoContentVideo;

/**
 * 项目名称：NotoginsengManage
 * 
 * 类名称：NewsCallAction
 * 
 * 类描述：
 * 
 * 创建人：李兴建
 * 
 * E-mail:lxj08193@163.com
 * 
 * 创建时间：2012-11-4 上午12:17:24
 * 
 * 修改人：李兴建
 * 
 * 修改时间：2012-11-4 上午12:17:24
 * 
 * 修改备注：
 * 
 * @version
 */
public class NewsCallAction extends BasePhoneAction {
	private int startNum = 0;
	private int limit = 0;
	private Long htmlViewId;

	private NgNewsInfo newsInfo;
	private String contentDetial;

	private Long optionId;
	private Long typeId;

	/**
	 * 信息分类 只返回六个
	 */
	public void newsType() {
		Criteria criteria = this.createCriteria(NgNewsCategory.class);
		criteria.add(Restrictions.eq("state", 0));
		criteria.addOrder(Order.asc("oder"));
		criteria.setFirstResult(0);
		criteria.setMaxResults(6);

		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "createAdminid", "createTime", "discription" });
		writResponse(JSONSerializer.toJSON(criteria.list(), config).toString());

	}

	/**
	 * 信息头 只取最新三张
	 */
	public void newsTitleList() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("head", 1));
		criteria.add(Restrictions.eq("state", 1));
		criteria.addOrder(Order.desc("oderView"));
		criteria.addOrder(Order.desc("createTime"));
		criteria.setFirstResult(0);
		criteria.setMaxResults(3);
		List<NgNewsInfo> newsInfo = criteria.list();
		for (int i = 0; i < newsInfo.size(); i++) {
			newsInfo.get(i).setHeadImg(getProjectURL() + newsInfo.get(i).getHeadImg());
		}
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "newsCategory", "createAdmin", "createTime", "publishTime", "state", "oderView", "remark" });
		writResponse(JSONSerializer.toJSON(newsInfo, config).toString());
	}

	/**
	 * 信息列表
	 */
	public void newsList() {
		NgNewsCategory newsCategory = this.getBaseService().findById(typeId, NgNewsCategory.class);
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("state", 1));
		criteria.add(Restrictions.eq("infotype", 0));
		criteria.add(Restrictions.eq("newsCategory", newsCategory));
		criteria.addOrder(Order.desc("oderView"));
		criteria.addOrder(Order.desc("createTime"));
		criteria.setFirstResult(startNum);
		criteria.setMaxResults(limit);
		List<NgNewsInfo> newsInfo = criteria.list();

		for (int i = 0; i < newsInfo.size(); i++) {
			newsInfo.get(i).setHeadImg(getProjectURL() + newsInfo.get(i).getHeadImg());
		}
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "newsCategory", "createAdmin", "createTime", "publishTime", "state", "oderView", "remark" });
		writResponse(JSONSerializer.toJSON(newsInfo, config).toString());
		System.out.println(JSONSerializer.toJSON(newsInfo, config).toString());
	}

	/**
	 * 图组list
	 */
	public void newsPictureList() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("state", 1));
		criteria.add(Restrictions.eq("infotype", 1));
		criteria.addOrder(Order.desc("oderView"));
		criteria.addOrder(Order.desc("createTime"));
		criteria.setFirstResult(startNum);
		criteria.setMaxResults(limit);
		List<NgNewsInfo> newsInfo = criteria.list();
		for (int i = 0; i < newsInfo.size(); i++) {
			newsInfo.get(i).setHeadImg(getProjectURL() + newsInfo.get(i).getHeadImg());
		}
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "newsCategory", "createAdmin", "createTime", "publishTime", "state", "oderView", "remark" });
		writResponse(JSONSerializer.toJSON(newsInfo, config).toString());
		System.out.println(JSONSerializer.toJSON(newsInfo, config).toString());
	}

	public void picturesList() {
		List<NgNewsInfoContentPicture> lists = this.getBaseService().findByProperty("newsInfoId", optionId, NgNewsInfoContentPicture.class);
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "createTime", "newsInfoId" });
		writResponse(JSONSerializer.toJSON(lists, config).toString());
	}

	/**
	 * 视频列表信息访问
	 */
	public void videoList() {
		Criteria criteria = this.createCriteria(NgNewsInfo.class);
		criteria.add(Restrictions.eq("state", 1));
		criteria.add(Restrictions.eq("infotype", 2));
		criteria.addOrder(Order.desc("oderView"));
		criteria.addOrder(Order.desc("createTime"));
		criteria.setFirstResult(startNum);
		criteria.setMaxResults(limit);
		List<NgNewsInfo> newsInfo = criteria.list();

		for (int i = 0; i < newsInfo.size(); i++) {
			newsInfo.get(i).setHeadImg(getProjectURL() + newsInfo.get(i).getHeadImg());
		}
		JsonConfig config = new JsonConfig();
		config.setIgnoreDefaultExcludes(false);
		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// 只要设置这个数组，指定过滤哪些字段。
		config.setExcludes(new String[] { "newsCategory", "createAdmin", "createTime", "publishTime", "state", "oderView", "remark" });
		writResponse(JSONSerializer.toJSON(newsInfo, config).toString());
		System.out.println(JSONSerializer.toJSON(newsInfo, config).toString());
	}

	/**
	 * 视频详细信息访问
	 */
	public String videoInfo() {
		newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
		NgNewsInfoContentVideo nmewsInfoContentVideo = null;
		List<NgNewsInfoContentVideo> list = this.getBaseService().findByProperty("newsInfoId", optionId, NgNewsInfoContentVideo.class);
		if (list.size() > 0) {
			nmewsInfoContentVideo = list.get(0);
			nmewsInfoContentVideo.setVideoAddress(getProjectURL() + nmewsInfoContentVideo.getVideoAddress());
			contentDetial=nmewsInfoContentVideo.getDescription();
		}
		return "newsDetilInfo";
//		JsonConfig config = new JsonConfig();
//		config.setIgnoreDefaultExcludes(false);
//		config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
//		// 只要设置这个数组，指定过滤哪些字段。
//		config.setExcludes(new String[] { "id", "remark", "beanState", "createTime" });
//		writResponse(JSONSerializer.toJSON(nmewsInfoContentVideo.getDescription()).toString());
		// writResponse(JSONSerializer.toJSON(nmewsInfoContentVideo,config).toString());
	}

	/**
	 * 视频详细信息访问地址
	 */
	public void videoInfoDownPath() {
		NgNewsInfoContentVideo nmewsInfoContentVideo = null;
		List<NgNewsInfoContentVideo> list = this.getBaseService().findByProperty("newsInfoId", optionId, NgNewsInfoContentVideo.class);
		if (list.size() > 0) {
			nmewsInfoContentVideo = list.get(0);
			nmewsInfoContentVideo.setVideoAddress(getProjectURL() + nmewsInfoContentVideo.getVideoAddress());

		}
		// JsonConfig config = new JsonConfig();
		// config.setIgnoreDefaultExcludes(false);
		// config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		// // 只要设置这个数组，指定过滤哪些字段。
		// config.setExcludes(new String[] { "createTime" });
		// writResponse(JSONSerializer.toJSON(nmewsInfoContentVideo.getDescription()).toString());
		writResponse("{\"videoAddress\":\"" + nmewsInfoContentVideo.getVideoAddress().toString() + "\",\"videoName\":\"" + nmewsInfoContentVideo.getVideoTitle() + "\"}");
	}

	/**
	 * 响应网页请求
	 */
	public String newsHtmlView() {
		try {
			newsInfo = this.getBaseService().findById(optionId, NgNewsInfo.class);
			List<NgNewsInfoContentHtml> list = this.getBaseService().findByProperty("newsInfoId", optionId, NgNewsInfoContentHtml.class);
			NgNewsInfoContentHtml newsInfoContentHtml = new NgNewsInfoContentHtml();
			if (list.size() > 0) {
				newsInfoContentHtml = list.get(0);
				contentDetial = newsInfoContentHtml.getHtmlContent();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "newsDetilInfo";
		// writResponse(newsInfoContentHtml.getHtmlContent());
	}

	/**
	 * @return the startNum
	 */
	public int getStartNum() {
		return startNum;
	}

	/**
	 * @param startNum
	 *            the startNum to set
	 */
	public void setStartNum(int startNum) {
		this.startNum = startNum;
	}

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @return the htmlViewId
	 */
	public Long getHtmlViewId() {
		return htmlViewId;
	}

	/**
	 * @param htmlViewId
	 *            the htmlViewId to set
	 */
	public void setHtmlViewId(Long htmlViewId) {
		this.htmlViewId = htmlViewId;
	}

	/**
	 * @return the optionId
	 */
	public Long getOptionId() {
		return optionId;
	}

	/**
	 * @param optionId
	 *            the optionId to set
	 */
	public void setOptionId(Long optionId) {
		this.optionId = optionId;
	}

	/**
	 * @return the typeId
	 */
	public Long getTypeId() {
		return typeId;
	}

	/**
	 * @param typeId
	 *            the typeId to set
	 */
	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	/**
	 * @return the contentDetial
	 */
	public String getContentDetial() {
		return contentDetial;
	}

	/**
	 * @param contentDetial
	 *            the contentDetial to set
	 */
	public void setContentDetial(String contentDetial) {
		this.contentDetial = contentDetial;
	}

	/**
	 * @return the newsInfo
	 */
	public NgNewsInfo getNewsInfo() {
		return newsInfo;
	}

	/**
	 * @param newsInfo
	 *            the newsInfo to set
	 */
	public void setNewsInfo(NgNewsInfo newsInfo) {
		this.newsInfo = newsInfo;
	}

}
